package org.social.controller;

import java.net.InetSocketAddress;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.bean.SourceChatMsg;
import org.constant.ChatConstant;
import org.error.GMErrorCode;
import org.social.db.bean.Notice;
import org.social.manager.NoticeManager;
import org.social.module.ChatManager;

import com.dal.gm_api.api.INotice;
import com.dal.gm_api.util.GMError;
import com.sojoys.artifact.tools.ToolError;

/**
 * 通知
 * Created by fxf on 2018-1-9.
 */
public class NoticeServer implements INotice{

    public void addNotice(int channel, int channelAppId, String content,String param,Date startTime, Date endTime, int idx, int type, int tag,String title, String contextImg){
        //必须
        GMError.isAndTrue(GMErrorCode.PARAM_ERR,"content 为空",content == null || "".equals(content));
        GMError.isAndTrue(GMErrorCode.PARAM_ERR,"title 为空",title == null || "".equals(title));
        GMError.isAndTrue(GMErrorCode.PARAM_ERR,"startTime 为空",startTime == null);
        GMError.isAndTrue(GMErrorCode.PARAM_ERR,"endTime 为空",endTime == null);


        Notice notice = new Notice(0,channel,channelAppId,idx,type,tag,title,content,param,contextImg,startTime,endTime);
        NoticeManager.addNotice(notice);
    }

    public void deleteNotice(List<Integer> idList){
        ToolError.isAndTrue(GMErrorCode.PARAM_ERR,"deleteNotice -> ids参数不正确",idList == null || idList.size() ==0);
        NoticeManager.deleteNotice(idList, true);
    }

    public List<String> queryNotice(boolean history){
        List<Notice> list = NoticeManager.getNotice(history);
        return list.stream().map(notice->notice.toJson()).collect(Collectors.toList());
    }

    public void modifyNotice(int id, int channel, int channelAppId, String content,String param,Date startTime, Date endTime, int idx, int type, int tag,String title, String contextImg){
            //必须
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"content 为空",content == null || "".equals(content));
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"title 为空",title == null || "".equals(title));
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"startTime 为空",startTime == null);
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"endTime 为空",endTime == null);
            Notice notice = NoticeManager.getNotice(id);
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"notice 为空  id="+id,notice == null);
            
            notice.setChannel(channel);
            notice.setChannelAppId(channelAppId);
            notice.setContent(content);
            notice.setParam(param);
            notice.setStartTime(startTime);
            notice.setEndTime(endTime);
            notice.setIdx(idx);
            notice.setType(type);
            notice.setTag(tag);
            notice.setTitle(title);
            notice.setContextImg(contextImg);
            NoticeManager.modifyNotice(notice);
    }

	@Override
	public void sendSystemChat(String contentKey) {
		SourceChatMsg msg = new SourceChatMsg(0, "System", 0, ChatConstant.CHANNEL_SYSTEM,ChatConstant.FUN_CHAT, contentKey,0);
		ChatManager.me().sendChat(msg);
	}

}
